package per.forest.database.meta;

import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;

class SqlJavaTypeTool
{
  public static Class<?> sqlTypeToJavaType(int sqlType)
  {
    if (isString(sqlType))
      return String.class;
    if (isBigDecimal(sqlType))
      return BigDecimal.class;
    if (isBoolean(sqlType))
      return Boolean.class;
    if (isInteger(sqlType))
      return Integer.class;
    if (isLong(sqlType))
      return Long.class;
    if (isFloat(sqlType))
      return Float.class;
    if (isDouble(sqlType))
      return Double.class;
    if (isByteArray(sqlType))
      return Byte.class;
    if (isDate(sqlType))
      return Date.class;
    if (isTime(sqlType))
      return Time.class;
    if (isTimestamp(sqlType))
      return Timestamp.class;
    if (isBlob(sqlType))
      return Blob.class;
    if (isClob(sqlType)) {
      return Clob.class;
    }
    return Object.class;
  }

  private static boolean isString(int sqlType)
  {
    return (sqlType == 1) || (sqlType == 12) || 
      (sqlType == -1);
  }

  private static boolean isBigDecimal(int sqlType)
  {
    return (sqlType == 2) || (sqlType == 3);
  }

  private static boolean isBoolean(int sqlType)
  {
    return sqlType == -7;
  }

  private static boolean isInteger(int sqlType)
  {
    return (sqlType == -6) || (sqlType == 5) || 
      (sqlType == 4);
  }

  private static boolean isLong(int sqlType)
  {
    return sqlType == -5;
  }

  private static boolean isFloat(int sqlType)
  {
    return sqlType == 7;
  }

  private static boolean isDouble(int sqlType)
  {
    return (sqlType == 6) || (sqlType == 8);
  }

  private static boolean isByteArray(int sqlType)
  {
    return (sqlType == -2) || (sqlType == -3) || 
      (sqlType == -4);
  }

  private static boolean isDate(int sqlType)
  {
    return sqlType == 91;
  }

  private static boolean isTime(int sqlType)
  {
    return sqlType == 92;
  }

  private static boolean isTimestamp(int sqlType)
  {
    return sqlType == 93;
  }

  private static boolean isBlob(int sqlType)
  {
    return sqlType == 2004;
  }

  private static boolean isClob(int sqlType)
  {
    return sqlType == 2005;
  }
}